<template>
  <div>
    <ul class="list">
      <li v-for="(item, index) in resultData" :key="index">
        <div class="title">
          <span class="classify"
            >【<i class="iconfont icon-zongguichengguo"></i>成果】</span
          >
          <h5 v-html="item['achievement_name']" @click="toDetail(item)"></h5>
        </div>
        <p v-html="item['achievement_profile']" class="content"></p>
        <p class="other">
          <span
            v-show="item.achievement_institution !== ''"
            :title="`第一完成单位：${item.achievement_institution}`"
            >{{ item.achievement_institution }}</span
          >
          <span
            v-show="item.contrib_full_name_short5 !== ''"
            :title="`成果完成人：${item.contrib_full_nameStr}`"
            >{{ item.contrib_full_name_short5 }}</span
          >
          <span
            v-show="item.registered_date"
            :title="`注册日期：${item.registered_date}`"
            >{{ item.registered_date }}</span
          >
        </p>
        <!-- 收藏 -->
        <!-- <div class="collect-btn" @click="collet(item, index)">
          <span class="iconfont"
            :class="{'active': item.isCollect, 'icon-shoucang-': item.isCollect, 'icon-shoucang': !item.isCollect}"
          ></span>
          <span v-if="item.isCollect" class="label">已收藏</span>
        </div> -->
      </li>
      <!-- no-data -->
      <div v-if="noData" class="no-data">
        <loading1 :emptyText="emptyText" />
      </div>
    </ul>
    <el-pagination
      v-if="resultData.length > 0"
      @current-change="handleCurrentChange"
      layout="prev, pager, next"
      :current-page.sync="opts['ipi']"
      :page-size="opts['ips']"
      :total="total"
      class="elpagination"
    ></el-pagination>
    <AchievementDialog ref="achievementDialog" />
  </div>
</template>

<script>
import AchievementDialog from "./AchievementDialog";
import { thousands, wordsLight } from "@/utils/utils";
export default {
  components: {
    AchievementDialog
  },
  data() {
    return {
      opts: {},
      resultData: [],
      total: 0,
      noData: false,
      emptyText: "",
      facetParams: {} // 聚合过来的参数
    };
  },
  methods: {
    init(opts) {
      this.opts = JSON.parse(JSON.stringify(opts));
      //
      if (this.opts.resetFacet) {
        this.facetParams = {};
      }
      this.search();
    },
    search() {
      this.resultData = [];
      this.noData = true;
      this.emptyText = "loading";
      let params = {
        ipi: this.opts.ipi,
        ips: this.opts.ips
      };
      if (this.opts.sort !== -1) {
        params["sortOrder"] = this.opts.sort;
      }
      // 聚合的参数
      if (Object.values(this.facetParams).length > 0) {
        params["filter"] = this.facetParams;
      }
      // 判断是否有高级检索
      if (this.opts.advanced) {
        if (this.opts.accurateSearch !== "") {
          params["accurateSearch"] = this.opts.accurateSearch;
        }
        if (this.opts.includeSearch !== "") {
          params["includeSearch"] = this.opts.includeSearch;
        }
        if (this.opts.notIncludeSearch !== "") {
          params["notIncludeSearch"] = this.opts.notIncludeSearch;
        }
      } else {
        if (this.opts.searchTxt !== "") {
          params["keyword"] = this.opts.searchTxt;
        }
      }

      this.$post(this.$Url.IR.achievement, params)
        .then(res => {
          let data = res.data;
          let result = data.result || [];
          if (result.length <= 0) {
            this.emptyText = "无法找到你所查询的内容，换个关键词试试";
          } else {
            this.noData = false;
          }
          // 配置高亮
          let settings = [{ words: this.opts.searchTxt, color: "#FF8C00" }];
          result.forEach(v => {
            v.achievement_name = wordsLight(v.achievement_name, settings);
            if (v.achievement_profile && v.achievement_profile !== "") {
              let lastStr =
                v.achievement_profile[v.achievement_profile.length - 1];
              let reg = /[.|。|？]/g;
              if (!reg.test(lastStr)) {
                v.achievement_profile += "...";
              }
            } else {
              v.achievement_profile = "暂无内容";
            }
            v.achievement_profile = wordsLight(v.achievement_profile, settings);
            v.contrib_full_name_short5 = "";
            if (v.contrib_full_name && v.contrib_full_name.length > 0) {
              // 成果完成人
              v.contrib_full_nameStr = v.contrib_full_name.join(" | ");

              let contrib_full_name_short5 = v.contrib_full_name.filter(
                (b, j) => j < 5
              );
              if (
                v.contrib_full_name.length > contrib_full_name_short5.length
              ) {
                contrib_full_name_short5 =
                  contrib_full_name_short5.join(" | ") + "...";
              } else {
                contrib_full_name_short5 = contrib_full_name_short5.join(" | ");
              }
              v.contrib_full_name_short5 = contrib_full_name_short5;
            }
          });
          this.resultData = result;
          let _total = data.total || 0;
          this.total = _total;
          this.$emit("resultTotal", thousands(_total));
          // 聚合
          if (this.opts.resetFacet) {
            let facetData = {
              achievement_type: data.achievement_type,
              achievement_stage: data.achievement_stage,
              promotion_way: data.promotion_way,
              achievement_field: data.achievement_field,
              application_status: data.application_status,
              registered_date: data.registered_date
            };
            this.$Bus.$emit("facetData", facetData);
          }
        })
        .catch(err => {
          this.emptyText = "无法找到你所查询的内容，换个关键词试试";
        });
    },
    // 是否收藏
    isCollect(eid, sth) {
      this.$get(this.$Url.company.isCollect2, {
        eid
      })
        .then(res => {
          let data = res.data;
          this.$set(sth, "isCollect", data);
        })
        .catch(err => {});
    },
    // 收藏
    collet(item, index) {
      return;
      if (item.isCollect) {
        // 取消收藏
        this.$get(this.$Url.company.cancelCollectComp, {
          eid: item.eid
        })
          .then(res => {
            if (res.status == 200) {
              this.$message({
                type: "success",
                message: "取消收藏成功"
              });
              this.$set(this.resultData[index], "isCollect", !item.isCollect);
            } else {
              this.$message({
                type: "error",
                message: "取消收藏失败",
                duration: 1000
              });
            }
          })
          .catch(e => {
            console.log(e);
          });
      } else {
        this.$get(this.$Url.company.collectComp, {
          eid: item.eid
        })
          .then(res => {
            if (res.status == 200) {
              this.$message({
                type: "success",
                message: "收藏成功",
                duration: 1000
              });
              this.$set(this.resultData[index], "isCollect", !item.isCollect);
            } else {
              this.$message({
                type: "error",
                message: "收藏失败",
                duration: 1000
              });
            }
          })
          .catch(e => {
            console.log(e);
          });
      }
    },
    // 分页
    handleCurrentChange(val) {
      this.opts.ipi = val;
      this.search();
    },
    // 在进入企业详情之前，先判断后台是否生成数据
    toDetail(item) {
      this.$refs.achievementDialog.init(item);
    }
  },
  mounted() {
    this.$Bus.$on("facetParams", params => {
      this.facetParams = params;
      this.$set(this.opts, "ipi", 1);
      this.search();
    });
  },
  beforeDestroy() {
    this.$Bus.$off("facetParams");
  }
};
</script>

<style lang="scss" scoped>
.list {
  min-height: 1.3rem;
  li {
    position: relative;
    padding-bottom: 0.2rem;
    border-bottom: 1px solid $color36;
    .title {
      position: relative;
      line-height: 0.48rem;
      padding-left: 0;
      padding-right: 0.6rem;
      border-bottom: 0;
      .classify {
        display: inline-block;
        min-width: 0.85rem;
        margin-right: 0.05rem;
        font-size: 0.14rem;
        vertical-align: middle;
        background-image: linear-gradient(50deg, $color55, $color56);
        background-clip: text;
        color: transparent;
        i {
          display: inline-block;
          padding: 0 0.04rem;
        }
      }
      h5 {
        display: inline-block;
        width: calc(100% - 1.5rem);
        // font-family: SourceHanSansCN-Medium;
        font-weight: normal;
        font-size: 0.18rem;
        vertical-align: middle;
        color: $color57;
        cursor: pointer;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        ::v-deep {
          em {
            color: $color120;
          }
        }
      }
    }
    .content {
      display: -webkit-box;
      max-height: 0.96rem;
      line-height: 0.24rem;
      margin: 0.1rem 0;
      font-size: 0.15rem;
      color: $color58;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 4; //控制行数
      overflow: hidden;
      ::v-deep {
        em {
          color: $color120;
        }
      }
    }
    .other {
      position: relative;
      display: flex;
      align-items: center;
      width: 100%;
      padding-top: 0.04rem;
      span {
        position: relative;
        display: inline-block;
        line-height: 0.24rem;
        padding: 0 0.06rem;
        margin-right: 0.1rem;
        font-size: 0.12rem;
        &:nth-of-type(1) {
          color: $color66;
          background-color: $color67;
        }
        &:nth-of-type(2) {
          color: $color62;
          background-color: $color63;
        }
        &:nth-of-type(3) {
          color: $color64;
          background-color: $color65;
        }
      }
    }
    .collect-btn {
      position: absolute;
      top: 0.14rem;
      right: 0;
      display: block;
      font-size: 0.16rem;
      line-height: 0.2rem;
      text-align: center;
      color: $color120;
      cursor: pointer;
      span {
        display: inline-block;
        vertical-align: middle;
        &.iconfont {
          font-size: 0.24rem;
        }
        &.label {
          margin-left: 0.05rem;
          font-size: 0.16rem;
        }
      }
      &.score {
        background-color: #8696e8;
      }
      &.el-icon-star-off {
        font-size: 18px;
        background: #909399;
        cursor: pointer;
      }
      &.active {
        font-size: 21px;
        background-color: #8696e8;
      }
    }
  }
}
.no-data {
  line-height: 1.2rem;
}
.elpagination {
  padding: 0.2rem 0;
  ::v-deep {
    .el-pager {
      li {
        font-weight: normal;
        &.active {
          font-size: 16px;
          font-weight: bold;
          color: $mainColor;
        }
      }
    }
    .more + .number {
      display: none;
    }
  }
}
</style>
